package com.framework.modules.sys.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.common.utils.Constant;
import com.framework.dao.YHBDao.sys.YhbSysMenuDao;
import com.framework.modules.sys.service.YhbSysMenuService;
import com.framework.modules.sys.service.YhbSysRoleMenuService;
import com.framework.modules.sys.service.YhbSysUserService;
import com.framework.utils.MapUtils;
import com.framework.yhbEntity.sys.YhbSysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("yhbSysMenuService")
public class YhbSysMenuServiceImpl extends ServiceImpl<YhbSysMenuDao,YhbSysMenu> implements YhbSysMenuService {

    @Autowired
    private YhbSysUserService yhbSysUserService;
    @Autowired
    private YhbSysRoleMenuService yhbSysRoleMenuService;

    @Override
    public List<YhbSysMenu> queryListParentId(Long parentId, List<Long> menuIdList) {
        List<YhbSysMenu> menuList = queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        List<YhbSysMenu> userMenuList = new ArrayList<>();
        for (YhbSysMenu menu : menuList) {
            if (menuIdList.contains(menu.getYhbMenuId())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<YhbSysMenu> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    @Override
    public List<YhbSysMenu> queryNotButtonList() {
        return baseMapper.queryNotButtonList();
    }

    @Override
    public List<YhbSysMenu> getUserMenuList(Long userId) {
        // 系统管理员，拥有最高权限
        if (userId == Constant.SUPER_ADMIN) {
            return getAllMenuList(null);
        }

        // 用户菜单列表
        List<Long> menuIdList = yhbSysUserService.queryAllMenuId(userId);
        return getAllMenuList(menuIdList);
    }

    @Override
    public void delete(Long menuId) {
        // 删除菜单
        this.deleteById(menuId);
        // 删除菜单与角色关联
        yhbSysRoleMenuService.deleteByMap(new MapUtils().put("yhb_sys_menu_id", menuId));
    }

    /**
     * 获取所有菜单列表
     */
    private List<YhbSysMenu> getAllMenuList(List<Long> menuIdList) {
        // 查询根菜单列表
        List<YhbSysMenu> menuList = queryListParentId(0L, menuIdList);
        // 递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     */
    private List<YhbSysMenu> getMenuTreeList(List<YhbSysMenu> menuList, List<Long> menuIdList) {
        List<YhbSysMenu> subMenuList = new ArrayList<>();

        for (YhbSysMenu entity : menuList) {
            // 目录
            if (entity.getType() == Constant.MenuType.CATALOG.getValue()) {
                entity.setList(getMenuTreeList(queryListParentId(entity.getYhbMenuId(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }
}
